<?php
namespace app\controller;

use app\BaseController;
use app\model\Admin as AD;
use think\validate\ValidateRule;
use think\facade\Session;

class Index extends BaseController
{
    protected $middleware = [
        // 对所有方法有效
        '\app\middleware\NavCheck',
    ];
    public function index()
    {
        if($this->request->isPost()){
            $data = $this->request->post();
            $user = AD::where('admin_name', $data['admin_name'])->find();
            if(empty($user)){
                return json(['code'=>"400","msg"=>"没有该用户名称"]);
            }

            if($user['admin_pwd'] != md5($data['admin_pwd'])){
                return json(['code'=>"400","msg"=>"密码错误"]);
            }
            Session::set('admin',$user);
            //return redirect('admin/index');
            return json(['code'=>"0","msg"=>"登录成功"]);
        }
        return view();
    }

    public function welcome(){
        $user = Session::get('admin');
        if(empty($user)){
            //$this->error("未登陆，请登录","index");
            return redirect("index");
        }
        return view('welcome',['admin'=>$user]);
    }

}
